package com.zznode.redsun.smartcrm.business.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.zznode.csp.domain.commons.persistence.EntityManager;
import com.zznode.csp.domain.commons.query.Page;
import com.zznode.redsun.commons.dto.ProductDto;
import com.zznode.redsun.commons.dto.ProductQuery;
import com.zznode.redsun.commons.utils.AbstractBaseAction;
import com.zznode.redsun.commons.utils.GlobalUtils;
import com.zznode.redsun.smartcrm.business.service.ProductService;
import com.zznode.redsun.smartcrm.model.Product;
import com.zznode.redsun.smartcrm.model.Provider;

@Controller
@Namespace("/business/product")
public class ProductAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;

	@Autowired
	private ProductService productService;

	@Autowired
	private EntityManager entityManager;

	private Page<ProductDto> pageData;
	private ProductDto product;
	private ProductQuery params;

	@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp") })
	public String index() {
		loadCategories();
		return SUCCESS;
	}

	@Action(value = "query", results = { @Result(name = SUCCESS, location = "query.jsp") })
	public String query() {
		loadCategories();
		return SUCCESS;
	}

	@Action(value = "list", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = { "root", "pageData",
			"excludeNullProperties", "true" }) })
	public String list() {
		this.pageData = productService.query(params, getPageNumber(), getPageSize());
		return SUCCESS;
	}

	@Action(value = "addNew", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String addNew() {
		this.product = new ProductDto();
		this.product.setCode(GlobalUtils.generateCode(null));

		loadCategories();
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String edit() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			this.product = productService.get(Long.valueOf(id));
		} else {
			return addNew();
		}

		loadCategories();
		return SUCCESS;
	}

	@Action(value = "save", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String save() {
		if (null == this.product) {
			this.setJsonMessage("保存失败，提交数据为空!");
		} else {
			this.productService.save(product);
		}
		return SUCCESS;
	}

	@Action(value = "remove", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String remove() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			this.entityManager.deleteById(Product.class, Long.valueOf(id));
		} else {
			this.setJsonMessage("删除失败，提交数据为空!");
		}
		return SUCCESS;
	}

	private void loadCategories() {
		this.putValueStack("providers", this.entityManager.getAll(Provider.class));
	}

	public Page<ProductDto> getPageData() {
		return pageData;
	}

	public void setPageData(Page<ProductDto> pageData) {
		this.pageData = pageData;
	}

	public ProductDto getProduct() {
		return product;
	}

	public void setProduct(ProductDto product) {
		this.product = product;
	}

	public ProductQuery getParams() {
		return params;
	}

	public void setParams(ProductQuery params) {
		this.params = params;
	}
}
